package com.moneywelcome.goldassembly.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moneywelcome.goldassembly.mapper.GoldTrendMapper;
import com.moneywelcome.goldassembly.model.GoldTrend;
import com.moneywelcome.goldassembly.service.GoldTrendService;
import com.moneywelcome.exceptions.MwcArgsException;
import com.moneywelcome.goldfacade.dto.GoldTrendDto;
import com.moneywelcome.goldfacade.dto.TrendDto;
import com.moneywelcome.goldfacade.vo.GoldTrendVo;
import com.moneywelcome.goldfacade.vo.TrendVo;
import com.moneywelcome.results.StatusCode;
import org.springframework.stereotype.Service;


/**
 * <p>
 * 黄金走势表 服务实现类
 * </p>
 *
 * @author alice
 * @since 2022-01-06
 */
@Service
public class GoldTrendServiceImpl extends ServiceImpl<GoldTrendMapper, GoldTrend> implements GoldTrendService {

    @Override
    public TrendDto queryTrend(TrendVo trendVo) {
        QueryWrapper<GoldTrend> qw=new QueryWrapper<>();
        qw.eq("gold_id",trendVo.getGoldId());
        qw.ge("trend_days",trendVo.getStartTime());
        qw.le("trend_days",trendVo.getEndTime());
        TrendDto trendDto = BeanUtil.copyProperties(trendVo, TrendDto.class);
        trendDto.setTrend(BeanUtil.copyToList( list(qw), GoldTrendDto.class));
        return trendDto;
    }
    @Override
    public GoldTrendDto create(GoldTrendVo goldTrendVo) {
        GoldTrend trend = BeanUtil.copyProperties(goldTrendVo, GoldTrend.class);
        trend.setTrendDays(DateUtil.today());
        if(!save(trend)) throw  new MwcArgsException("数据保存失败", StatusCode.Error);
        return BeanUtil.copyProperties(trend,GoldTrendDto.class);
    }

    @Override
    public GoldTrendDto queryTrendDay(Integer goldId, String today) {
        GoldTrend trend = getOne(new QueryWrapper<GoldTrend>().eq("gold_id", goldId)
                .eq("trend_days", today));

        return BeanUtil.copyProperties(trend,GoldTrendDto.class);
    }
}
